package com.swang.pms.service.manage.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.swang.pms.dal.ManageRoleDao;
import com.swang.pms.model.common.GridUtil;
import com.swang.pms.model.common.Pager;
import com.swang.pms.model.dto.ManageRoleDto;
import com.swang.pms.model.entity.ManageRole;
import com.swang.pms.service.manage.ManageRoleService;

/**
 * @ClassName: ManageRoleServiceImpl
 * @Description: 角色服务实现类
 * @author 王泽
 * @date 2017年3月16日 下午9:45:51
 */
@Service("manageRoleService")
public class ManageRoleServiceImpl implements ManageRoleService {

    @Resource
    private ManageRoleDao manageRoleDao;

    public void addManageRole(ManageRole ManageRole) {
        this.manageRoleDao.insert(ManageRole);
    }

    public void addManageRoleSelective(ManageRole ManageRole) {
        this.manageRoleDao.insertSelective(ManageRole);
    }

    public void deleteManageRoleByKey(Long ManageRoleId) {
        this.manageRoleDao.deleteByPrimaryKey(ManageRoleId);
    }

    public void updateManageRoleByKey(ManageRole ManageRole) {
        this.manageRoleDao.updateByPrimaryKey(ManageRole);
    }

    public void updateManageRoleByKeySelective(ManageRole ManageRole) {
        this.manageRoleDao.updateByPrimaryKeySelective(ManageRole);
    }

    public void updateManageRoleByDto(ManageRoleDto ManageRoleDto) {
    }

    public ManageRole selectManageRoleByKey(Long ManageRoleId) {
        return this.manageRoleDao.selectByPrimaryKey(ManageRoleId);
    }

    public int addManageRoleByDto(ManageRoleDto dto) {
        return 0;
    }

    public List<ManageRoleDto> selectsBySelf(ManageRoleDto dto) {
        return this.manageRoleDao.selectsBySelf(dto);
    }

    public void saveMenus(Long roleId, Long[] menus) {
        if (roleId == null || menus == null || menus.length <= 0) {
            return;
        }
        this.manageRoleDao.deleteAllMenusByRoleId(roleId);
        for (Long menuId : menus) {
            ManageRoleDto dto = new ManageRoleDto();
            dto.setRoleId(roleId);
            dto.setMenuId(menuId);
            this.manageRoleDao.insertRoleMenu(dto);
        }
    }

    public List<Long> getMenuIdsByRoleId(Long roleId) {
        return this.manageRoleDao.getMenuIdsByRoleId(roleId);
    }

    //------------------------增删改查--------------------------------
    //扩展
    public int countRole(ManageRole po) {
        if (po == null) {
            return 0;
        }
        return this.manageRoleDao.countRole(po);
    }

    //分页查询
    public Pager findPageDataByDto(Pager page, ManageRoleDto dto) {

        String[] paramSerial = new String[] { "id", "role_name", "remark", "status", "sort_num", "gmt_create" };
        dto.setColumns(Arrays.asList(paramSerial).toString().replaceAll("[\\[\\]\\s]", ""));
        dto.setStartIndex(page.getRowStartIndex());
        dto.setPageSize(page.getPageSize());
        List<Map<String, Object>> list = this.manageRoleDao.findPageDataByDto(dto);

        dto.setStartIndex(null);
        dto.setPageSize(null);
        page.setRowCount(this.manageRoleDao.findPageDataCountByDto(dto));
        page.setGridr(GridUtil.getGridList(list, paramSerial, page.getRowStartIndex(), page.getPageSize(), page.getRowCount(), true));
        return page;
    }

}
